package homework.no_4;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class ProDom4j {

    private static Document doc;
    private static final String PATH = "src/main/java/homework/no_4/Products.xml";

    static {
        try {
            doc = new SAXReader().read(new File(PATH));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void showInfo() {
        Element root = doc.getRootElement();
        for (Iterator<Element> it = root.elementIterator(); it.hasNext(); ) {
            Element id = it.next();
            System.out.println(id.attributeValue("productno"));
            for (Iterator chiNode = id.elementIterator(); chiNode.hasNext(); ) {
                Element node = (Element) chiNode.next();
                System.out.println("\t" + node.getText());
            }
        }
    }

    public static void saveXml(String path) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        try {
            XMLWriter writer = new XMLWriter(new FileWriter(path), format);
            writer.write(doc);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void searchPro(int num) {
        Element root = doc.getRootElement();
        Element element = root.elements().get(num);
        for (Iterator chiNode = element.elementIterator(); chiNode.hasNext(); ) {
            Element node = (Element) chiNode.next();
            System.out.println("\t" + node.getText());
        }
    }

    public static void update(int num, String count) {
        Element root = doc.getRootElement();
        Element element = root.elements().get(num-1).element("stockCount");
        element.setText(count);
        saveXml(PATH);
        showInfo();
    }

    public static void buyPro(int num, int count){
        Element root = doc.getRootElement();

        Element element = root.elements().get(num-1).element("price");
        Double price = Double.parseDouble(element.getText());//获得价格

        Element element1 = root.elements().get(num-1).element("discount");//获得折扣值
        Double discount = Double.parseDouble(element1.getText());

        Double total = price * count * discount;//总价


        Element count1 = root.elements().get(num - 1).element("stockCount");//获得库存数
        int stock = Integer.parseInt(element.getText());

        int stockCount = stock - count;
        if (stockCount< 0){
            System.out.println("库存不足！");
        }
        count1.setText(String.valueOf(stockCount));
        System.out.println("你购买了"+num+"件商品，总价为"+total+"元");

        saveXml(PATH);
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 50; i++) {
        showInfo();
            System.out.print("请输入你要产看的产品序号:");
            int num = sc.nextInt();
        searchPro(num);
            if (num < 3) {
                System.out.print("请输入修改的库存数:");
                String count = sc.next();
                update(num, count);
                break;
            } else {
                System.out.println("没有该序号的产品请输入1-3之间的整数");
            }
        }

        showInfo();
        System.out.print("输入你要购买的商品编号：");
        int num = sc.nextInt();
        System.out.print("输入你要购买的商品的数量：");
        int count = sc.nextInt();
        buyPro(num,count);
    }
}
